<template>
    <div class="login-container">
        <el-form class="card-box login-form" ref="loginForm" label-position="left" :rules="rules" :model="loginForm">
            <img class="logo" :src="img_logo" alt="logo">
            <h3 class="title" v-text="msg"></h3>
            <el-form-item prop="account">
                <span class="svg-container svg-container_login">
                    <i class="el-icon-wallet-user"></i>
                </span>
                <el-input type="text" @keyup.enter.native="submit('loginForm')" v-model="loginForm.account" autoComplete="on" placeholder="账号" />

            </el-form-item>

            <el-form-item prop="passwd">
                <span class="svg-container">
                    <i class="el-icon-wallet-password"></i>
                </span>
                <el-input type="password" @keyup.enter.native="submit('loginForm')" v-model="loginForm.passwd" autoComplete="on" placeholder="密码" />
            </el-form-item>

            <el-button class="login-btn" style="width: 100%; margin-bottom: 30px;" @click="submit('loginForm')">登录</el-button>

        </el-form>
    </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex';
import md5 from 'js-md5';
import img_logo from '@/assets/wallet/logo.png'

export default {
    name: 'login',
    data() {
        return {
            msg: 'Hcash 彩链管理系统',
            img_logo,
            loginForm: {
                account: '',
                passwd: ''
            },
            rules: {
                account: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                ],
                passwd: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                ],
            }
        }
    },
    computed: {
        ...mapGetters(['G_TOKENKEY']),
        ...mapState(['StateUserAuth']),
    },
    mounted() {

    },
    methods: {
        ...mapActions(['A_SET_TOKENKEY', 'A_USER_SIGNIN', 'A_USER_AUTH_LEVEL']),
        submit(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$http({
                        url     : 'issue/api/v1/issue/user/login',
                        method  : 'post',
                        body    : {
                            account: this.loginForm.account,
                            passwd: md5(this.loginForm.passwd)
                        }
                    }).then((res) => {
                        // console.log(res.data);
                        let data = res.data;
                        if (data.responseCode === 1) {
                            this.A_SET_TOKENKEY(data.responseData.token);//保存token
                            this.A_USER_SIGNIN(data.responseData.account);//保存用户
                            this.A_USER_AUTH_LEVEL(data.responseData.type);//保存用户授权等级
                            // 默认登录后进列表页面
                            this.$router.push({ name: 'agency' });
                        } else {
                            this.$notify({
                                title: '登录失败',
                                message: data.responseData,
                                type: 'error',
                                duration: 2000
                            });
                        }
                    })
                }

            })
        }
    }
}
</script>

<style rel="stylesheet/less" lang="less">
@bg: -webkit-gradient(linear, 0 0, 0 bottom, from(#7e2d81), to(#5d3f8c));
@dark_gray: #fff;
@light_gray: #fff;

body,
html {
    height: 100%;
}

.login-container {
    height: 100%;
    text-align: center;
    background: @bg;
    overflow: hidden;
    img.logo {
        margin: 0 auto 40px 0;
        width: 120px;
    }
    button.login-btn {
        background-color: transparent;
        border: 1px solid #fff;
        color: white;
    }
    input:-webkit-autofill {
        -webkit-box-shadow: 0 0 0 1000px #293444 inset !important;
        -webkit-text-fill-color: #fff !important;
    }
    input {
        background: transparent;
        border: 0;
        -webkit-appearance: none;
        border-radius: 0;
        padding: 12px 5px 12px 15px;
        color: @light_gray;
        height: 47px;
    }
    .el-input {
        display: inline-block;
        height: 47px;
        width: 85%;
    }
    .tips {
        font-size: 14px;
        color: #fff;
        margin-bottom: 10px;
    }
    .svg-container {
        padding: 6px 5px 6px 15px;
        color: @dark_gray;
        vertical-align: middle;
        width: 30px;
        display: inline-block;
        line-height: 1px;
        &_login {
            font-size: 20px;
        }
    }
    .title {
        font-size: 26px;
        font-weight: 400;
        color: @light_gray;
        margin: 0 auto 40px;
        text-align: center;
        font-weight: bold;
    }
    .login-form {
        width: 400px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        transform-origin: 50% 50%;
        -webkit-transform: translate(-50%, -50%);
        -webkit-transform-origin: 50% 50%;
    }
    .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }
    .show-pwd {
        position: absolute;
        right: 10px;
        top: 7px;
        font-size: 16px;
        color: @dark_gray;
        cursor: pointer;
    }
    .thirdparty-button {
        position: absolute;
        right: 35px;
        bottom: 28px;
    }
}
</style>
